﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace ILN.MVC.Datetimepicker
{
    public static class DatetimepickerExtensions
    {
        public static MvcHtmlString DateTimeFor<TModel, TValue>(
                   this HtmlHelper<TModel> html,
                   Expression<Func<TModel, TValue>> expression,
                   DatetimepickerOptions options = null)
        {

            // Get the Metadata from Model's DataAnnotations.
            var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
            string divWrapperId = metadata.PropertyName + "_d";
            // Main input.
            TagBuilder input = DateTimePickerExtensionUtils.BuildMainInput(metadata);

            TagBuilder hiddenData = DateTimePickerExtensionUtils.BuildDataElement(metadata, divWrapperId, options);

            // Adds the validation properties from the HelperMethods.HTMLHelper package.
            DateTimePickerExtensionUtils.AddValidationProperties(html, expression, metadata, input);

            TagBuilder span = DateTimePickerExtensionUtils.BuildSpanElement();
            TagBuilder divWrapper = DateTimePickerExtensionUtils.BuildDivWrapper(divWrapperId);

            divWrapper.InnerHtml = string.Concat(input, span);

            return new MvcHtmlString(string.Concat(hiddenData, divWrapper));

        }

        public static MvcHtmlString DateTimeFor<TModel, TValue>(
                    this HtmlHelper<TModel> html,
                    Expression<Func<TModel, TValue>> expression)
        {

            return DateTimeFor(html, expression, new DatetimepickerOptions());
        }
    }
}